package com.iflytek.uoamp.notify.core.dump;

import java.io.File;
import java.io.FilenameFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NotificationFileFilter implements FilenameFilter {
	
    private long currentTime = 0;
    private static final String FORMAT = "yyyy-MM-dd_HH:mm";
    
    public NotificationFileFilter(long now) {
        currentTime = now;
    }

	@Override
	public boolean accept(File dir, String name) {
	    String regex = "content_(.+)\\.dump";
	    Pattern pattern = Pattern.compile(regex);
	    Matcher matcher = pattern.matcher(name);
	    SimpleDateFormat formatter = new SimpleDateFormat(FORMAT);
	    
	    if(!matcher.matches()) {
            return false;
	    }
	    
	    try {
            Date now = new Date(currentTime);
            Date fileTime = formatter.parse(matcher.group(1));
            if(fileTime.before(now)) {
                return true;
            }
        } catch (ParseException e) {
            return false;
        }

		return false;
	}
}